﻿// This is an open source non-commercial project. Dear PVS-Studio, please check it.
// PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com

// ReSharper disable CheckNamespace
// ReSharper disable CommentTypo
// ReSharper disable IdentifierTypo
// ReSharper disable LocalizableElement
// ReSharper disable MemberCanBePrivate.Global
// ReSharper disable UnusedAutoPropertyAccessor.Global
// ReSharper disable UnusedMember.Global
// ReSharper disable UnusedType.Global

/* BusyControllerTest.cs --
 * Ars Magna project, http://arsmagna.ru
 */

#region Using directives

using System.Drawing;
using System.Threading;
using System.Windows.Forms;

using AM.Threading;
using AM.Windows.Forms;

#endregion

#nullable enable

namespace FormsTests;

public sealed class BusyControllerTest
    : IFormsTest
{
    #region IFormsTest members

    public void RunTest
        (
            IWin32Window? ownerWindow
        )
    {
        using var form = new Form
        {
            Size = new Size (800, 600)
        };

        var firstButton = new Button
        {
            Text = "Push me 1",
            Location = new Point (10, 100)
        };
        form.Controls.Add (firstButton);

        var secondButton = new Button
        {
            Text = "Push me 2",
            Location = new Point (200, 100)
        };
        form.Controls.Add (secondButton);

        var state = new BusyState();

        var stripe = new BusyStripe
        {
            Dock = DockStyle.Top,
            ForeColor = Color.LimeGreen,
            Height = 20,
            Text = "I am very busy"
        };
        stripe.SubscribeTo (state);
        form.Controls.Add (stripe);

        var controller = new BusyController (state);
        controller.Controls.Add (firstButton);
        controller.Controls.Add (secondButton);
        controller.ExceptionOccur += (_, args) => { ExceptionBox.Show (ownerWindow, args.Exception); };

        void SleepAction()
        {
            Thread.Sleep (3000);
        }

        firstButton.Click += (_, _) => { controller.Run (SleepAction); };

        secondButton.Click += (_, _) => { controller.RunAsync (SleepAction); };

        form.ShowDialog (ownerWindow);
    }

    #endregion
}
